<?php
    require_once './register.class.php';
    if (checkSession()) {
        returnToPage('index.php');
    }
    $type = (isset($_GET['type']) && in_array($_GET['type'], array('login', 'register')))?$_GET['type']:'register';
    if (!empty($_POST['name'])) {
        $name = trim($_POST['name']);
        $number = intval($_POST['number']);
        $email = trim($_POST['email']);
        $money = intval($_POST['money']);
        $face = $_FILES['face'];
        $info = trim($_POST['info']);
        $reg = new Register($name, $number, $email, $money, $face, $info);
        $res = $reg->sqlInsert();
        if ($res) {
            $_SESSION['user']['name'] = $name;
            $_SESSION['user']['number'] = $number;
            $_SESSION['user']['id'] =  $res;
            returnToPage('index.php');
        }
    }
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>注册</title>
    <link href="style/css/bootstrap.min.css" rel="stylesheet">
    <link href="style/css/site.min.css" rel="stylesheet">
</head>

<body>
    <!--导航栏-->
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand hidden-sm" href="index.php">慕课网</a>
            </div>
        </div>
    </div>
    <!--导航栏结束-->
    <!-- 注册页面 -->
    <?php if ($type == 'register') : ?>
    <div class="container projects">
        <div class="projects-header page-header">
            <h3>注册</h3>
        </div>
        <!--注册框-->
        <div class="row">
          <div class="col-md-6 col-md-offset-3">
            <form class="form-horizontal" action="register.php" method="post" enctype="multipart/form-data">
              <div class="form-group">
                  <label for="inputEmail3" class="col-sm-2 control-label">姓名</label>
                  <div class="col-sm-10">
                      <input type="text" class="form-control" name="name" placeholder="请输入姓名">
                  </div>
              </div>
              <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">学号</label>
                  <div class="col-sm-10">
                      <input type="text" class="form-control" name="number" placeholder="请输入学号">
                  </div>
              </div>
              <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">邮箱</label>
                  <div class="col-sm-10">
                      <input type="email" class="form-control" name="email" placeholder="请输入正确邮箱">
                  </div>
              </div>
              <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">金钱</label>
                  <div class="col-sm-10">
                      <input type="text" class="form-control" name="money" placeholder="请输入数字">
                  </div>
              </div>
              <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">头像</label>
                  <div class="col-sm-10">
                      <input type="file" name="face">
                  </div>
              </div>
              <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">个人简介</label>
                  <div class="col-sm-10">
                    <textarea class="form-control" name="info" rows="2"  placeholder="不超过50字"></textarea>
                  </div>
              </div>
              <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                      <button type="submit" class="btn btn-primary btn-default">提交</button>
                      <button type="submit" class="btn btn-default">重置</button>
                  </div>
              </div>
              </form>
          </div>
        </div>

        <!--登录框-->
        <!-- 登录页面 -->
        <?php elseif ($type == 'login'): ?>
        <div class="projects-header page-header">
            <h3>登录</h3>
        </div>
        <!-- 登录框 -->
        <div class="row" >
            <div class="col-md-6 col-md-offset-3">
                <form class="form-horizontal" action="login.class.php" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" placeholder="请输入姓名,字数不超过10位" name="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-2 control-label">学号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" placeholder="请输入学号,数字不超过8位" name="number">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-primary btn-default">提交</button>
                            <button type="reset" class="btn btn-default">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <?php endif;?>
    <footer class="footer  container">
        <div class="row footer-bottom">
            <ul class="list-inline text-center">
                <h4><a href="http://class.imooc.com" target="_blank">class.imooc.com</a> | 慕课网</h4>
            </ul>
        </div>
    </footer>
    <?php if (!empty($_GET['msg'])) {
    $msg = $_GET['msg'];
    echo "<script>alert('$msg')</script>";
} ?>
    <script src="style/js/jquery.min.js"></script>
    <script src="style/js/bootstrap.min.js"></script>
</body>

</html>
